
static const float Power = 8.0;
static const float Bailout = 5.0f;
static const int   Iterations = 19;


float3 powN(float3 p, float r)
{
	float phi = atan(p.y / p.x);
	float theta = acos(p.z / r);

	float3 res;

	res.x = sin(Power*theta) * cos(Power*phi);
	res.y = sin(Power*theta) * sin(Power*phi);
	res.z = cos(Power*theta);

	return res;
}

int Icosahedron(float x, float y, float z,int Iter)
{
	int escape=-1;

	float V[20];//variables
	float CT[20];//C Value
	float radius=0;
	int ic, it;

	const float delta=0;

	//20 dimensions
	CT[0]=abs(x+y+z)-delta;
	CT[1]=abs(x+y -z)-delta;
	CT[2]=abs(x -y +z)-delta;
	CT[3]=abs(x -y -z)-delta;

	CT[4]=abs(-x+ y +z)-delta;
	CT[5]=abs(-x +y -z)-delta;
	CT[6]=abs(-x -y +z)-delta;
	CT[7]=abs(-x -y -z)-delta;

	CT[8]=abs(y*0.61803398874989484820458683436564+ z*1.6180339887498948482045868343656)-delta;
	CT[9]=abs(y*0.61803398874989484820458683436564 -z*1.6180339887498948482045868343656)-delta;
	CT[10]=abs(-y*0.61803398874989484820458683436564 +z*1.6180339887498948482045868343656)-delta;
	CT[11]=abs(-y*0.61803398874989484820458683436564 -z*1.6180339887498948482045868343656)-delta;

	CT[12]=abs(x*0.61803398874989484820458683436564+ y*1.6180339887498948482045868343656)-delta; 
	CT[13]=abs(x*0.61803398874989484820458683436564 -y*1.6180339887498948482045868343656)-delta;
	CT[14]=abs(-x*0.61803398874989484820458683436564 +y*1.6180339887498948482045868343656)-delta;
	CT[15]=abs(-x*0.61803398874989484820458683436564 -y*1.6180339887498948482045868343656)-delta; 

	CT[16]=abs(x*1.6180339887498948482045868343656 + z*0.61803398874989484820458683436564)-delta;
	CT[17]=abs(x*1.6180339887498948482045868343656  -z*0.61803398874989484820458683436564)-delta;
	CT[18]=abs(-x*1.6180339887498948482045868343656  +z*0.61803398874989484820458683436564)-delta;
	CT[19]=abs(-x*1.6180339887498948482045868343656  -z*0.61803398874989484820458683436564)-delta;
	
	for(it=0;it<20;it++)
		V[it]=0;

	for (ic=0;ic<Iter;ic++)
	{
		radius=0;
		for(int it=0;it<20;it++)
		{
			if(V[it]>1)
				V[it]=2-V[it];
			else if(V[it]<-1)
				V[it]=-2-V[it];
	    
			 radius+=(V[it]*V[it]);
		 }

		const float vm=sqrt(radius);

		if(vm<0.5)
		{
			for(int it=0;it<20;it++)
				V[it]*=4;
		} 
		else if(vm<1.75)
		{
			const float vsq=vm*vm;
			for(int it=0;it<20;it++)
				V[it]/=vsq;
		}
		radius=0;
		
		for(it=0;it<20;it++)
		{
			V[it]=V[it]*2+CT[it];
			radius+=(V[it]*V[it]);
		}	
		if (radius>3600) break;
	 }
		if(ic<Iter)
			escape=-1;
		else 
			escape=1;

	return escape;
}






int inside(float3 pos) 
{
	float3 z=pos;
	float r;
	int i=0;
	r=length(z);
	while(r<Bailout && (i<Iterations)) {
		pos = powN(z,r);
		z+=pos;
		r=length(z);
		i++;
	}
	
	if (r<Bailout)
		return 1;
	else 
		return -1;

}